/*
 * Copyright (c) 2015—2030 GantSoftware.Co.Ltd. All rights reserved.
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * is not allowed to be distributed or copied without the license from
 * GantSoftware.Co.Ltd. Please contact the company for more information.
 */
package com.gantang.gax.service.impl;

import com.gantang.gax.dto.bool.BoolFeatureTable;
import com.gantang.gax.engine.eo.Engine;
import com.gantang.gax.engine.model.EngineCreator;
import com.gantang.gax.engine.model.VariableGroup;
import com.gantang.gax.engine.service.LogicEngineService;
import com.gantang.gax.engine.service.EngineCreatorService;
import com.gantang.gax.service.BoolFeatureTableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("logicBoolFeatureTableService")
public class FeatureTableServiceImpl implements BoolFeatureTableService {
    @Autowired
    private EngineCreatorService logicEngineCreatorService;

    @Override
    public Engine doUpsertFeatureTable(BoolFeatureTable featureTable) {
        EngineCreator engineCreator = featureTable2EngineCreator(featureTable);
        return logicEngineCreatorService.create(engineCreator);
    }

    @Override
    public Object doValidateFeatureTable(BoolFeatureTable featureTable) {
        return null;
    }

    /**
     * 此处将featureTable业务转为TaskObject
     *
     * @param featureTable
     * @return
     */
    private EngineCreator featureTable2EngineCreator(BoolFeatureTable featureTable) {
        EngineCreator engineCreator = new EngineCreator();
        // 初始化 variableMap
        Map<String, List<VariableGroup.Variable>> variableMap = new HashMap<>();
        // 初始化 variableMap2
        Map<String, List<String>> variableMap2 = new HashMap<>();
        // 遍历 features 列表，填充 variableMap 和 variableMap2
        for (BoolFeatureTable.LogicFeature feature : featureTable.getFeatures()) {
            // 创建 Variable 对象
            VariableGroup.Variable variable = VariableGroup.Variable.builder().code(feature.getCode()).weight(feature.getWeight()).build();
            // 获取 familyCode 作为 key
            String familyCode = feature.getFamilyCode();
            // 将 Variable 添加到对应 familyCode 的列表中
            variableMap.computeIfAbsent(familyCode, k -> new ArrayList<>()).add(variable);
            // 将 code 添加到 variableMap2 的对应 familyCode 列表中
            variableMap2.computeIfAbsent(familyCode, k -> new ArrayList<>()).add(feature.getCode());
        }
        // 设置 engineTask 的 variableMap
        engineCreator.setVariableGroupMap(variableMap);
        // 初始化 rules 列表
        List<String> rules = new ArrayList<>();
        // 遍历 rules 列表，填充 rules
        for (BoolFeatureTable.LogicRule rule : featureTable.getRules()) {
            rules.add(rule.getExpression());
        }
        // 设置 engineTask 的 rules
        engineCreator.setFormulas(rules);
        return engineCreator;
    }
}
